package cn.yitun.chat.yitunserverimapicore.business.group.action;

import cn.yitun.chat.yituncommonmessage.ExceptionEnum;
import cn.yitun.chat.yituncommonmessage.YitunException;
import cn.yitun.chat.yituncommonmessage.result.ResultMessage;
import cn.yitun.chat.yituncommonutil.YitunCollectionUtil;
import cn.yitun.chat.yitungeneral.annotation.action.ActionMapping;
import cn.yitun.chat.yitungeneral.annotation.parameter.JsonDefine;
import cn.yitun.chat.yitunnetsession.session.SocketSession;
import cn.yitun.chat.yitunservercommonlib.constant.ActionTypeConstant;
import cn.yitun.chat.yitunservercommonlib.dataentity.dto.group.GroupSettingDto;
import cn.yitun.chat.yitunservercommonlib.dataentity.vo.group.GroupJoinVerifyQuestionVo;
import cn.yitun.chat.yitunservercommonlib.dataentity.vo.group.GroupSettingVo;
import cn.yitun.chat.yitunservercommonlib.util.NumUtil;
import cn.yitun.chat.yitunservercommonlib.util.R;
import cn.yitun.chat.yitunserverimapicore.business.group.service.GroupJoinSettingService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * Date 2019-01-20 11:55:08<br>
 * Description
 * 
 * @author yitun
 * @since 1.0.0
 */
@Component
@ActionMapping(value = ActionTypeConstant.GROUP_SETTING_ACTION_PUSH)
public class GroupSettingAction {

	@Resource
	private GroupJoinSettingService groupJoinSettingService;

	/**
	 * 获取详情
	 */
	@ActionMapping(value = "1.1.0001")
	public ResultMessage getDetail(SocketSession socketSession, @JsonDefine("data") GroupSettingDto query) {
		if (NumUtil.isInValid(query.getGroupId()))
			throw new YitunException(ExceptionEnum.LOSE_FILED);

		GroupSettingVo data = groupJoinSettingService.getByGroupId(query.getGroupId());
		List<GroupJoinVerifyQuestionVo> list = groupJoinSettingService.getVerifyQuestionListByGroupId(query.getGroupId());
		return new ResultMessage(R.param("joinSetting", data).put("questionList", list));
	}

	/**
	 * 更新
	 */
	@ActionMapping(value = "1.1.0002")
	public ResultMessage update(SocketSession socketSession, @JsonDefine("data") GroupSettingDto query) {
		if (YitunCollectionUtil.isEmpty(query.getGroupJoinVerifyQuestionList()))
			throw new YitunException(ExceptionEnum.LOSE_FILED);
		Long userId = socketSession.getKey();
		ResultMessage message = new ResultMessage();
		message.setInfo(groupJoinSettingService.saveOrUpdate(userId, query));
		return message;
	}
}
